package ui;

import java.util.logging.Logger;

import javax.swing.Timer;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import components.PumpController;

public class DosageConfim extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_dosage;
	PumpController pc;
	
	private boolean bExitFlag = false;
	private Shell parent_shell;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public DosageConfim(Shell parent, int style) {
		super(parent, style);
		setText("Confimation");
		parent_shell = parent;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open(String _dosage, PumpController _pc) {
		createContents();
		pc = _pc;
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();	
		Point location = parent_shell.getLocation();		// Get the location of the parent. *SR
		shell.setLocation(location.x+68, location.y+26); 	// Move popup to sit on top of main composite
		text_dosage.setText(_dosage);						// Set the correct dosage amount
		while (!shell.isDisposed() && bExitFlag == false) { // *SR
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		shell.dispose(); // *SR
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(360, 284);
		shell.setText(getText());
		
		text_dosage = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.RIGHT);
		text_dosage.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		text_dosage.setText("0000000");
		text_dosage.setBounds(230, 103, 115, 36);
		
		Button btnAdminister = new Button(shell, SWT.NONE);
		btnAdminister.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bExitFlag = true;
				
				pc.getInsulinManager().initiateBolus();
				
				//TODO: Logging
				Logger log = Logger.getLogger("log");
				log.info("Bolus Administration: " + text_dosage.getText());
			}
		});
		btnAdminister.setBounds(191, 195, 154, 51);
		btnAdminister.setText("Administer");
		
		Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bExitFlag = true;
			}
		});
		btnCancel.setBounds(10, 195, 154, 51);
		btnCancel.setText("Cancel");
		
		Label lblConfimBolus = new Label(shell, SWT.NONE);
		lblConfimBolus.setFont(SWTResourceManager.getFont("Segoe UI", 33, SWT.BOLD));
		lblConfimBolus.setAlignment(SWT.CENTER);
		lblConfimBolus.setBounds(10, 10, 335, 69);
		lblConfimBolus.setText("Confim Bolus");
		
		Label lblAdministerTheFollowing = new Label(shell, SWT.WRAP);
		lblAdministerTheFollowing.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.NORMAL));
		lblAdministerTheFollowing.setBounds(10, 73, 335, 51);
		lblAdministerTheFollowing.setText("Administer the following amount of insulin? (in Units)");

	}

}
